package com.zmn.oms.business.impl.perf;

import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.business.interfaces.perf.PerfQueryBService;
import com.zmn.oms.common.constant.DimensionTypeEnum;
import com.zmn.oms.common.constant.MeasureTypeEnum;
import com.zmn.oms.model.entity.perf.*;
import com.zmn.oms.model.vo.perf.PerfDistributeVO;
import com.zmn.oms.services.interfaces.perf.PerfDimensionDistributeService;
import com.zmn.oms.services.interfaces.perf.PerfFactService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.zmn.oms.common.constant.MeasureTypeEnum.*;

/**
 * PerfDistributeQueryBServiceImpl
 *
 * @author hankangshuai
 * @since 2019/11/20
 **/
@Service("perfDistributeQueryBService")
public class PerfDistributeQueryBServiceImpl implements PerfQueryBService<PerfDistributeVO, PerfDimensionDistribute, PerfDimensionDistributeQuery> {
    @Resource
    PerfDimensionDistributeService perfDimensionDistributeService;
    @Resource
    PerfFactService perfFactService;

    /**
     * 根据条件查询纬度ID
     *
     * @param query 查询条件
     * @return 纬度ID集合
     */
    @Override
    public List<Integer> listDimensionIdByQuery(PerfDimensionDistributeQuery query) {
        return perfDimensionDistributeService.listDimensionIdByQuery(query);
    }

    /**
     * 查询纬度
     *
     * @param query 查询条件
     * @return 纬度列表
     */
    @Override
    public List<PerfDimensionDistribute> listDimensionPageByQuery(PerfDimensionDistributeQuery query) {
        int count = perfDimensionDistributeService.countByQuery(query);
        if (NumberUtil.isNullOrZero(count)) {
            return Collections.emptyList();
        }
        query.setTotalCount(count);
        return perfDimensionDistributeService.listPageByQuery(query);
    }

    /**
     * 根据纬度查询事实
     *
     * @param dimensionId   纬度ID集合
     * @param perfTimeQuery 绩效时间查询对象
     * @return 事实实体
     */
    @Override
    public List<PerfFact> listFactByDimensionId(List<Integer> dimensionId, PerfTimeQuery perfTimeQuery) {
        PerfFactQuery perfFactQuery = new PerfFactQuery();
        perfFactQuery.setDimensionType(DimensionTypeEnum.DISTRIBUTE);
        perfFactQuery.setFrom(perfTimeQuery.getFrom());
        perfFactQuery.setTo(perfTimeQuery.getTo());
        perfFactQuery.setDimensionIds(dimensionId);
        return perfFactService.listByQuery(perfFactQuery);
    }

    /**
     * 将纬度和事实合并成VO
     *
     * @param perfDimensionDistributeOpt 单个纬度
     * @param singleFact                 单条事实
     * @return VO
     */
    @Override
    public PerfDistributeVO mergeToVO(Optional<PerfDimensionDistribute> perfDimensionDistributeOpt, Map<MeasureTypeEnum, Long> singleFact) {
        PerfDimensionDistribute perfDimensionDistribute = perfDimensionDistributeOpt.orElseGet(() -> {
            PerfDimensionDistribute totalLine = new PerfDimensionDistribute();
            totalLine.setDistributorName("汇总");
            return totalLine;
        });
        PerfDistributeVO perfDistributeVO = new PerfDistributeVO();
        perfDistributeVO.setPlat(perfDimensionDistribute.getPlat());
        perfDistributeVO.setDeptId(perfDimensionDistribute.getDeptId());
        perfDistributeVO.setDistributorId(perfDimensionDistribute.getDistributorId());
        perfDistributeVO.setDistributorName(perfDimensionDistribute.getDistributorName());
        perfDistributeVO.setConfirmCount(Math.toIntExact(singleFact.getOrDefault(CONFIRM_COUNT, 0L)));
        perfDistributeVO.setDistributeCount(Math.toIntExact(singleFact.getOrDefault(DISTRIBUTE_COUNT, 0L)));
        perfDistributeVO.setDistributedCount(Math.toIntExact(singleFact.getOrDefault(DISTRIBUTED_COUNT, 0L)));
        perfDistributeVO.setRemarkCount(Math.toIntExact(singleFact.getOrDefault(REMARK_COUNT, 0L)));
        perfDistributeVO.setRemarkedCount(Math.toIntExact(singleFact.getOrDefault(REMARKED_COUNT, 0L)));
        perfDistributeVO.setTrackCount(Math.toIntExact(singleFact.getOrDefault(TRACK_COUNT, 0L)));
        perfDistributeVO.setTrackedCount(Math.toIntExact(singleFact.getOrDefault(TRACKED_COUNT, 0L)));
        perfDistributeVO.setDistributeSuccessCount(Math.toIntExact(singleFact.getOrDefault(DISTRIBUTE_SUCCESS_COUNT, 0L)));
        perfDistributeVO.setAbnormalHandlingCount(Math.toIntExact(singleFact.getOrDefault(ABNORMAL_HANDLING_COUNT, 0L)));
        perfDistributeVO.setComplaintHandlingCount(Math.toIntExact(singleFact.getOrDefault(COMPLAINT_HANDLING_COUNT, 0L)));
        return perfDistributeVO;
    }
}
